package com.xiaozeng.user.controller;

import com.xiaozeng.param.AddressParam;
import com.xiaozeng.param.AddressRemoveParam;
import com.xiaozeng.user.service.AddressService;
import com.xiaozeng.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: xiaozeng
 * @Date: 2022 11
 * @Description: 用户地址管理功能实现
 **/
@RestController
@RequestMapping("user/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 查询地址接口
     */
    @PostMapping("list")
    public Result list(){

        return addressService.list();
    }

    /**
     * 新建地址接口
     * @param address 新建的地址信息
     */
    @PostMapping("save")
    public Result save(@RequestBody AddressParam address){
        return addressService.save(address);
    }

    /**
     * 查询地址接口
     * @param addressRemoveParam 需要删除的地址参数
     */
    @PostMapping("remove")
    public Result remove(@RequestBody @Validated AddressRemoveParam addressRemoveParam, BindingResult bindingResult){
        //检查是否符合检验注解的规则  符合 false  不符合 true
        if (bindingResult.hasErrors()){
            return Result.fail("参数异常,删除失败!");
        }
        return addressService.remove(addressRemoveParam.getId());
    }
}
